package jatosoft.accounting.webapp.managedbean;

import javax.annotation.PostConstruct;

import jatosoft.accounting.dao.model.Account;
import jatosoft.accounting.service.AccountService;

public class AccountBean extends BaseManagedBean {
	private AccountService accountService;

	public void setAccountService(AccountService accountService) {
		this.accountService = accountService;
	}

	private Account account = new Account();

	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

	@Override
	@PostConstruct
	public void initPage() {
		dumpModel2UIModel(accountService.listAccount(null));
		account.setParentAccount(new Account());

	}

	public String saveAccount() {
//		if(account.getParentAccount().getAccountId()==null){
			account.setParentAccount(null);
//		}
		accountService.addAccount(account);
		dumpModel2UIModel(accountService.listAccount(null));
		account.setId(null);
		account.setAccountName(null);
		return "OK";
	}

	public String removeAccount() {
		Account a = (Account) getSelectModel();
		accountService.removeAccount(a);
		dumpModel2UIModel(accountService.listAccount(null));
		account = new Account();
		return "OK";
	}
}
